package com.cuanbo.dualscreenteachfu.matrix;

import com.cuanbo.dualscreenteachfu.base.App;
import com.cuanbo.dualscreenteachfu.bean.DataList;
import com.cuanbo.dualscreenteachfu.constant.MatrixConstant;
import com.cuanbo.dualscreenteachfu.service.BackgroundService;
import com.cuanbo.dualscreenteachfu.utils.BroadcastUtil;
import com.cuanbo.dualscreenteachfu.utils.LogUtil;

import java.io.IOException;
import java.io.OutputStream;
import java.net.SocketException;

/**
 * 给矩阵发送消息的线程
 */
public class MatrixSendThread extends Thread {
    private MatrixInitThread tcpInitThread;
    private OutputStream output;
    public static Object lock = new Object();
    private int reConnectCount;
    private boolean loop;

    public MatrixSendThread(MatrixInitThread tcpInitThread) {
        reConnectCount = 0;
        this.tcpInitThread = tcpInitThread;
        try {
            output = tcpInitThread.getSocket().getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        super.run();
        loop = true;
        LogUtil.add("发送", "启动发送线程-->" + Thread.currentThread().getName(), "矩阵");
        while (App.running && loop) {
            try {
                String strData = DataList.get();
                if (strData != null) {
                    output.write(strData.getBytes("utf-8"));
                    output.flush();
                    if (strData.contains(">APPS-START:") || strData.contains(">C")) {
                        LogUtil.add("发送", strData, "矩阵");
                    } else {
                        LogUtil.add("发送", strData, "矩阵发送_全部",false);
                    }
                }
            } catch (SocketException e) {
                BackgroundService.reConnectingMatrix = false;
                LogUtil.addError(e);
                tcpInitThread.close();
                LogUtil.add("MatrixSendThread(91)","matrixTcpInitThread.close()","关闭中控Tcp");
                if (e.getMessage().contains("Socket") && e.getMessage().contains("closed")) {//当socket已关闭时，还做读写操作报该错误
                    //Log.i("MatrixSendThread", "当socket已关闭时，还做读写操作报该错误SocketException Socket closed!");
                } else if (e.getMessage().contains("Broken") && e.getMessage().contains("pipe")) {//服务端断开连接时，继续进行写操作会报该错误
                    //尝试重新连接
                    BroadcastUtil.sendMatrixBroadcast(MatrixConstant.TYPE_MATRIX_RECONNECT);
                } else {
                    e.printStackTrace();
                    BroadcastUtil.sendMatrixBroadcast(MatrixConstant.TYPE_MATRIX_SEND_ERROR);
                }
            } catch (Exception e) {
                BackgroundService.reConnectingMatrix = false;
                LogUtil.addError(e);
                tcpInitThread.close();
                LogUtil.add("MatrixSendThread(106)","matrixTcpInitThread.close()","关闭中控Tcp");
                e.printStackTrace();
                BroadcastUtil.sendMatrixBroadcast(MatrixConstant.TYPE_MATRIX_SEND_ERROR);
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        LogUtil.add("发送", "关闭发送线程-->" + Thread.currentThread().getName(), "矩阵");
    }

    public void stopThread() {
        loop = false;
    }
}
